package kr.hkit.summart_java.serviceUI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.GridLayout;

import javax.swing.JLabel;

import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summart_java.dao.DaoInterface;
import kr.hkit.summart_java.dao.DepartmentDAO;
import kr.hkit.summart_java.erp.dto.Department;

import java.awt.BorderLayout;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.BoxLayout;

import java.awt.FlowLayout;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.awt.Component;

public class PanelDepartmentUI extends JPanel implements ActionListener {

	private InputPanel pDeptNo;
	private InputPanel pDeptName;
	private InputPanel pDeptFloor;
	private DaoInterface<Department> dao;
	private JLabel label;
	public static JButton btnadd;
	public static JButton btndel;
	public static JButton btnupd;
	public static JButton btnsear;
//	public static JButton btnlist;

	/**
	 * Create the panel.
	 */
	public PanelDepartmentUI() {

		initialize();
	}
	public void initialize() {
//	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel_Center = new JPanel();
		add(panel_Center);
		panel_Center.setLayout(new BoxLayout(panel_Center, BoxLayout.Y_AXIS));
		
		pDeptNo = new InputPanel("부서번호");
		panel_Center.add(pDeptNo);
		pDeptNo.setLayout(new GridLayout(1, 0, 0, 0));
		
		pDeptName = new InputPanel("부서명");
		panel_Center.add(pDeptName);
		pDeptName.setLayout(new GridLayout(1, 0, 0, 0));
		
		pDeptFloor = new InputPanel("위치");
		panel_Center.add(pDeptFloor);
		pDeptFloor.setLayout(new GridLayout(1, 0, 0, 0));
		
		label = new JLabel("");
		label.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		panel_Center.add(label);
		
		JPanel panel_Bottom = new JPanel();
		add(panel_Bottom, BorderLayout.SOUTH);
		
		btnadd = new JButton("");
		btnadd.addActionListener(this);
		panel_Bottom.add(btnadd);

	}

	public void actionPerformedbtnadd(ActionEvent e) {
		int dno = Integer.parseInt(pDeptNo.getTf().trim());
		String name = pDeptName.getTf();
		int floor =Integer.parseInt(pDeptFloor.getTf().trim());
		
		Department d = new Department(dno, name, floor);
		JOptionPane.showMessageDialog(this, d);
		try {
			dao.insertItem(d);
			JOptionPane.showMessageDialog(this, "입력되었습니다.");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "입력실패.");
		}
	}
	public void actionPerformedbtndel(ActionEvent e) {
		int dno = Integer.parseInt(pDeptNo.getTf().trim());
		String name = pDeptName.getTf();
		int floor =Integer.parseInt(pDeptFloor.getTf().trim());
		Department d = new Department(dno, name, floor);
		JOptionPane.showMessageDialog(this, d);
		try {
			dao.deleteItemByNo(d);
			JOptionPane.showMessageDialog(this, "삭제되었습니다.");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "삭제실패.");
		}
	}
	public void actionPerformedbtnupd(ActionEvent e) {
		int dno = Integer.parseInt(pDeptNo.getTf().trim());
		String name = pDeptName.getTf();
		int floor =Integer.parseInt(pDeptFloor.getTf().trim());
		Department d = new Department(dno, name, floor);
		JOptionPane.showMessageDialog(this, d);
		try {
			dao.updateItemByNo(d);
			JOptionPane.showMessageDialog(this, "수정되었습니다.");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "수정실패.");
		}
	}
	public void actionPerformedbtnsear(ActionEvent e) {
		Department item = null;
		int dno = Integer.parseInt(pDeptNo.getTf().trim());
		String name = pDeptName.getTf();
		int floor =Integer.parseInt(pDeptFloor.getTf().trim());
		Department d = new Department(dno,name,floor);
		JOptionPane.showMessageDialog(this, d);
		try {
			dao.searchItem(d);
			item = dao.searchItem(d);
			label.setText(item+"");
			JOptionPane.showMessageDialog(this, "검색되었습니다.");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "실패.");
		}
	}

	public void setDao(DaoInterface<Department> dao) {
		this.dao = dao;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(btnadd.getText()=="추가"){
			actionPerformedbtnadd(e);
		}else if(btnadd.getText()=="삭제"){
			actionPerformedbtndel(e);
		}else if(btnadd.getText()=="수정"){
			actionPerformedbtnupd(e);
		}else{
			actionPerformedbtnsear(e);
		}
		
	}
}
